package apis

import (
	"fmt"
	"strings"
	"vrcm/src/vrcm/app/admin/models/live_schedule"
	"vrcm/src/vrcm/app/admin/service/dto/live_schedule"
	"vrcm/src/vrcm/app/admin/service/live_schedule"
	"vrcm/src/vrcm/common/apis"

	"github.com/gin-gonic/gin"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
)

type ChannelBasic struct {
	apis.Api
}

// GetPage 获取ChannelBasic列表
// @Summary 获取ChannelBasic列表
// @Description 获取ChannelBasic列表
// @Tags ChannelBasic
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.ChannelBasicService}} "{"code": 200, "data": [...]}"
// @Router /api/v1/channel-basic [get]
// @Security Bearer
func (e ChannelBasic) GetPage(c *gin.Context) {
	req := dto.ChannelBasicGetPageReq{}
	s := service.ChannelBasicService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	list := make([]live_schedule.ChannelBasic, 0, 5)
	var count int64

	err = s.GetPage(&req, &list, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取通道失败， %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取ChannelBasic
// @Summary 获取ChannelBasic
// @Description 获取ChannelBasic
// @Tags ChannelBasic
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.ChannelBasicService} "{"code": 200, "data": [...]}"
// @Router /api/v1/channel-basic/{id} [get]
// @Security Bearer
func (e ChannelBasic) Get(c *gin.Context) {
	req := dto.ChannelBasicGetReq{}
	s := service.ChannelBasicService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	var object live_schedule.ChannelBasic

	err = s.Get1(&req, &object)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取通道失败， %s", err.Error()))
		return
	}

	e.OK(object, "查询成功")
}

// Insert 创建ChannelBasic
// @Summary 创建ChannelBasic
// @Description 创建ChannelBasic
// @Tags ChannelBasic
// @Accept application/json
// @Product application/json
// @Param data body dto.ChannelBasicInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/channel-basic [post]
// @Security Bearer
func (e ChannelBasic) Insert(c *gin.Context) {
	req := dto.ChannelBasicInsertReq{}
	s := service.ChannelBasicService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Insert(&req)

	if err != nil {
		if strings.Contains(err.Error(), "Duplicate entry") {
			e.Error(apis.AddSourceError.Code, err, fmt.Sprintf("创建通道失败， %s", "不能重复创建"))
		} else {
			e.Error(apis.AddSourceError.Code, err, fmt.Sprintf("创建通道失败， %s", err.Error()))
		}
		return
	}

	e.OK(req.GetId(), "创建成功")
}

// Update 修改ChannelBasic
// @Summary 修改ChannelBasic
// @Description 修改ChannelBasic
// @Tags ChannelBasic
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.ChannelBasicUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/channel-basic/{id} [put]
// @Security Bearer
func (e ChannelBasic) Update(c *gin.Context) {
	req := dto.ChannelBasicUpdateReq{}
	s := service.ChannelBasicService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	err = s.Update(&req)
	if err != nil {
		if strings.Contains(err.Error(), "Duplicate entry") {
			e.Error(apis.AddSourceError.Code, err, fmt.Sprintf("创建通道失败， %s", "不能重复创建"))
		} else {
			e.Error(apis.UpdateChannelError.Code, err, fmt.Sprintf("修改通道失败，\r\n失败信息 %s", err.Error()))
		}

		return
	}
	e.OK(req.GetId(), "修改成功")
}

// Delete 删除ChannelBasic
// @Summary 删除ChannelBasic
// @Description 删除ChannelBasic
// @Tags ChannelBasic
// @Param data body dto.ChannelBasicDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/channel-basic [delete]
// @Security Bearer
func (e ChannelBasic) Delete(c *gin.Context) {
	s := service.ChannelBasicService{}
	req := dto.ChannelBasicDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Remove(&req)
	if err != nil {
		e.Error(apis.DeleteChannelError.Code, err, fmt.Sprintf("删除任务失败，%s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}

func (e ChannelBasic) GetChannelsBySource(c *gin.Context) {
	s := service.ChannelBasicService{}
	req := dto.GetChannelsBySourceReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	data, err := s.GetChannelsBySource(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取通道失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(data, "获取调度通道成功")
}

func (e ChannelBasic) GetChannelTaskStatistics(c *gin.Context) {
	s := service.ChannelBasicService{}
	req := dto.GetChannelTaskStatisticsReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	data, err := s.GetChannelTaskStatistics(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取调度统计信息失败， %s", err.Error()))
		return
	}
	e.OK(data, "获取调度统计信息成功")
}

func (e ChannelBasic) CreateTimeTask(c *gin.Context) {
	s := service.ChannelBasicService{}
	req := dto.EnableOrCancelTimeTaskReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.EnableOrCancelTimeTask(&req)
	if err != nil {
		if err.Error() == string(apis.CreateTimeTaskError.Code) {
			e.Error(apis.CreateTimeTaskError.Code, err, "任务运行中不能更改定时任务")
		} else {
			e.Error(apis.EnableOrCancelTimeTaskError.Code, err, err.Error())
		}

		return
	}
	e.OK(nil, "启用或者取消定时通到成功")
}

func (e ChannelBasic) GetBasicInfo(c *gin.Context) {
	req := dto.ChannelBasicGetReq{}
	s := service.ChannelBasicService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	var object live_schedule.ChannelBasic

	err = s.Get(&req, &object)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取通道失败， %s", err.Error()))
		return
	}

	e.OK(object, "查询成功")
}

func (e ChannelBasic) StartOrStopTimeTask(c *gin.Context) {
	s := service.ChannelBasicService{}
	req := dto.StartOrStopTimeTaskReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.StartOrStopTimeTask(&req)
	if err != nil {
		e.Error(apis.EnableOrCancelTimeTaskError.Code, err, err.Error())
		return
	}
	e.OK(nil, "停用定时通到成功")
}

func (e ChannelBasic) SaveCycleTime(c *gin.Context) {
	s := service.ChannelBasicService{}
	req := dto.SaveCycleTimeReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.SaveCycleTime(&req)
	if err != nil {
		if err.Error() == string(apis.TimeError.Code) {
			e.Error(apis.TimeError.Code, err, "开始时间不能大于结束时间")
		} else if err.Error() == string(apis.WeekTimeError.Code) {
			e.Error(apis.TimeError.Code, err, "设置的时间不符合周期定时时间")
		} else {
			e.Error(apis.DBUpdateError.Code, err, err.Error())
		}

		return
	}
	e.OK(nil, "保存定期任务成功")
}

func (e ChannelBasic) GetCycleTime(c *gin.Context) {
	s := service.ChannelBasicService{}
	req := dto.GetCycleTimeReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	var count int64
	list := make([]live_schedule.CycleTime, 0, 5)
	err = s.GetCycleTime(&req, &list, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, err.Error())
		return
	}
	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "获取定期任务成功")
}

func (e ChannelBasic) DeleteCycleTime(c *gin.Context) {
	s := service.ChannelBasicService{}
	req := dto.DeleteCycleTimeReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.DeleteCycleTime(&req)
	if err != nil {
		e.Error(apis.DBDeleteError.Code, err, err.Error())
		return
	}
	e.OK(nil, "删除定期任务成功")
}

func (e ChannelBasic) GenerateCycleTime(c *gin.Context) {
	s := service.ChannelBasicService{}
	req := dto.GenerateCycleTimeReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.GenerateCycleTime(&req)
	if err != nil {
		e.Error(apis.DBCreateError.Code, err, err.Error())
		return
	}
	e.OK(nil, "增加定期任务成功")
}

func (e ChannelBasic) CancelCycleTime(c *gin.Context) {
	s := service.ChannelBasicService{}
	req := dto.CancelCycleTimeReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.CancelCycleTime(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, err.Error())
		return
	}
	e.OK(nil, "更新定期任务成功")
}
